package xfireservice.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import com.alibaba.fastjson.JSON;
import JianYan.bo.JianYanBO;
import xfireservice.StrUtils;
import xfireservice.service.IntelligentTerminalService;

public class   IntelligentTerminalServiceImpl implements  IntelligentTerminalService{
	private JianYanBO jybo=new JianYanBO();
    private static final String userID = "admin.android.dy";
    private static final String mykey = "B9C507A748246EF370CF165C71DC6706";
    private static final String url = "http://127.0.0.1:8080/sdjy";
    /**
     * 根据申报单号查询所申报的设备信息
     * code 申报单号
     * sign 【userId+所需要的参数+key】格式MD5加密的密钥
     */
	@Override
	public String getShenbaoInfo(String code,String sign) {
		try {
	        StringBuilder valueStr = new StringBuilder("userID=");
	        valueStr.append(userID);
	        if (code == null ||"".equals(code.trim())) {
	        	 return StrUtils.setErrorMsg("NODATE-申报单号code不能为空");
	        } 
	        valueStr.append("&code=").append(code.trim());
	        if (sign == null || "".equals(sign)) {
	            return StrUtils.setErrorMsg("NODATE-签名密钥sign不能为空");
	        }
	        //验证签名
	        valueStr.append("&key=").append(mykey);
	        String signServer = StrUtils.enCodeMd5(valueStr.toString().toLowerCase()).toUpperCase();
	        if(!sign.equals(signServer)) {
	        	return StrUtils.setErrorMsg("NODATE-签名密钥sign不正确");
	        }
			StringBuffer sql = new StringBuffer();
			sql.append("SELECT R.REPORT_NAME_ID REPORTID,R.ID BGID,T.ANNALID ID,T.UNIT_NAME SBDW,R.REPORT_CODE BGBH,T.SE_CODE ZCDM,T.SE_TYPE SBLX,");
			sql.append(" T.APPLY_TYPE JYLX,T.SE_NAME SBMC,T.MAKE_NUM CPBH,");
			sql.append(" T.TESTDEPART JYBM,T.FEE_ACTUAL_PRICE SFJE,T.SE_FLAG ZT,R.REPORT_PRINT_DATE DYRQ");
			sql.append(" FROM JY_SHENBAO_SE_LIST T LEFT JOIN REPORT_INFO_COLLECT R ON T.ANNALID=R.SE_ID");
			sql.append(" WHERE T.FORM_CODE='"+code.trim()+"'");
			Vector vector=jybo.getList(sql.toString());
			List<Map<String,String>>list=new ArrayList<Map<String,String>>();
			for (Object object : vector) {
				Map<String,String> map=(Map<String, String>) object;
				String sqldic="SELECT T.PRINT_JSP,T.PRINT_CMD,T.VIEW_NAME FROM REPORT_INFO_DICTIONARY T where t.id='"+map.get("REPORTID")+"'";
				Hashtable hash=jybo.getElement(sqldic);
				if(hash!=null) {
					String printurl=url+"/controller?nextpage="+hash.get("PRINT_JSP")+"&command="+hash.get("PRINT_CMD")+"&canexecute=true&ANNALID="+map.get("ID")+"&reportNameId="+map.get("REPORTID")+"&View="+hash.get("VIEW_NAME");
					map.put("PRINTURL", printurl);
				}
				if(map.get("DYRQ")==null||"".equals(map.get("DYRQ"))) {
					map.put("DYRQ", "未打印");
				}else {
					map.put("DYRQ", "已打印");
				}
				list.add(map);
			}
			Map<String,Object>map=new HashMap<String,Object>();
			if(list.size()==0) {
				map.put("status", "0");
				map.put("msg", "NODATE-无数据");
			}else {
				map.put("status", "1");
				map.put("msg", "OK");
			}
			map.put("list", list);
			return JSON.toJSONString(map);
		} catch (Exception e) {
			 return StrUtils.setErrorMsg("NOUPDATE-查询数据错误");
		}
	}
	  /**
     * 报检单位，报检日期时间段，报检人，报检电话来查询一个或者多个申报单号,
     * sign 【userId+所需要的参数+key】格式MD5加密的密钥
     */
	@Override
	public String getShenbaoForm(String condition, String sign) {
		try {
			StringBuilder valueStr = new StringBuilder("userID=");
	        valueStr.append(userID);
	        if (condition == null ||"".equals(condition.trim())) {
	        	 return StrUtils.setErrorMsg("NODATE-查询条件不能为空");
	        } 
	        if (sign == null || "".equals(sign)) {
	            return StrUtils.setErrorMsg("NODATE-签名密钥sign不能为空");
	        }
	        //验证签名
	        valueStr.append("&condition=").append(condition);
	        valueStr.append("&key=").append(mykey);
	        String signServer = StrUtils.enCodeMd5(valueStr.toString().toLowerCase()).toUpperCase();
	        if(!sign.equals(signServer)) {
	        	 return StrUtils.setErrorMsg("NODATE-签名密钥sign不正确");
	        }
        	Map<String, Object> conditionMap=JSON.parseObject(condition);
        	String sbdw=conditionMap.get("sbdw")!=null?conditionMap.get("sbdw").toString().trim():"";
        	String bjrq1=conditionMap.get("bjrq1")!=null?conditionMap.get("bjrq1").toString().trim():"";
        	String bjrq2=conditionMap.get("bjrq2")!=null?conditionMap.get("bjrq2").toString().trim():"";
        	String lxr=conditionMap.get("lxr")!=null?conditionMap.get("lxr").toString().trim():"";
        	String lxdh=conditionMap.get("lxdh")!=null?conditionMap.get("lxdh").toString().trim():"";
        	if("".equals(sbdw)) {
        		return StrUtils.setErrorMsg("NODATE-申报单位不能为空");
        	}
        	StringBuffer sql = new StringBuffer();
			sql.append("SELECT T.SHENBAOCODE SBDH,TO_CHAR(T.APPLY_DATE,'yyyy-MM-dd') SBRQ,T.UNIT_NAME SBDW,T.APPLY_PERSON LXR ,T.LINKER_SHOUJI LXDH");
			sql.append(" FROM JY_SHENBAO_FORM T");
			sql.append(" WHERE T.UNIT_NAME like '%"+sbdw+"%'");
			if(!"".equals(bjrq1)) {
				sql.append(" AND TO_CHAR(T.APPLY_DATE,'yyyy-MM-dd')>='"+bjrq1+"'");
        	}
			if(!"".equals(bjrq2)) {
				sql.append(" AND TO_CHAR(T.APPLY_DATE,'yyyy-MM-dd')<='"+bjrq2+"'");
        	}
			if(!"".equals(lxr)) {
				sql.append(" AND T.APPLY_PERSON like '%"+lxr+"%'");
        	}
			if(!"".equals(lxdh)) {
				sql.append(" AND T.LINKER_SHOUJI like '%"+lxdh+"%'");
        	}
	        Vector vector=jybo.getList(sql.toString());
	        Map<String,Object>map=new HashMap<String,Object>();
			if(vector==null||vector.size()==0) {
				map.put("status", "0");
				map.put("msg", "NODATE-无数据");
			}else {
				map.put("status", "1");
				map.put("msg", "OK");
			}
			map.put("list", vector);
			return JSON.toJSONString(map);
		} catch (Exception e) {
			return StrUtils.setErrorMsg("NOUPDATE-查询数据错误");
		}
	}
	/**
     * 根据主键值更新报告统计表打印日期状态
     * code 申报单号
     * sign 【userId+所需要的参数+key】格式MD5加密的密钥
     */
	@Override
	public String updateReportPrint(String id, String sign) {
		try {
			StringBuilder valueStr = new StringBuilder("userID=");
	        valueStr.append(userID);
	        if (id == null ||"".equals(id.trim())) {
	        	 return StrUtils.setErrorMsg("NODATE-报告id不能为空");
	        } 
	        if (sign == null || "".equals(sign)) {
	            return StrUtils.setErrorMsg("NODATE-签名密钥sign不能为空");
	        }
	        //验证签名
	        valueStr.append("&id=").append(id);
	        valueStr.append("&key=").append(mykey);
	        String signServer = StrUtils.enCodeMd5(valueStr.toString().toLowerCase()).toUpperCase();
	        if(!sign.equals(signServer)) {
	        	 return StrUtils.setErrorMsg("NODATE-签名密钥sign不正确");
	        }
        	StringBuffer sql = new StringBuffer();
        	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        	String now=sdf.format(new Date());
			sql.append("UPDATE REPORT_INFO_COLLECT SET REPORT_PRINT_DATE='"+now+"' WHERE ID='"+id.trim()+"'");
	        boolean flag=jybo.UpdSql(sql.toString());
	        Map<String,Object>map=new HashMap<String,Object>();
			if(flag) {
				map.put("status", "1");
				map.put("msg", "OK");
			}else {
				map.put("status", "0");
				map.put("msg", "NOUPDATE-更新数据失败");
			}
			return JSON.toJSONString(map);
		} catch (Exception e) {
			return StrUtils.setErrorMsg("NOUPDATE-更新数据错误");
		}
	} 
}
